## IFI maps
library(tidyverse)
library(sf)
library(ggrepel)
library(grid)
library(gridExtra)
library(cowplot)


### HQ map figure 2
loc<-read.csv("aio_lending_hqs_geocoded.csv",header=T)

df <- dplyr::select(loc, IFI,Longitude,Latitude,hybrid)

mapWorld <- borders("world", colour="gray83", fill="gray92")

df_sf <- st_as_sf(df, coords=c('Longitude', 'Latitude'), crs = 4326, remove = F)

ff<-ifelse(df_sf$hybrid==0,"bold","plain")
labels <- c(expression(paste(Consolidated)), expression(paste(Hybrid)))

plot<- ggplot(df_sf)+
  mapWorld +
  geom_sf() +scale_color_manual(values=c("maroon4", "maroon"),name="",labels=c("Consolidated","Hybrid"))+
  geom_text_repel(aes(x=Longitude, y=Latitude,label=IFI,color=as.factor(hybrid),fontface = ff ), size=13/.pt,point.padding = .1,
                  box.padding=.3, max.overlaps = 19,show.legend=F)+ theme_void() + 
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.title.x = element_blank(),
          axis.title.y = element_blank())
plot

tag1 <- textGrob("Consolidated", gp = gpar(fontsize = 14, fontface = "bold", col = "maroon4"))
tag2 <- textGrob("Hybrid", gp = gpar(fontsize = 14, fontface = "plain", col = "maroon"))


plot_with_tags <- plot_grid(
  plot, 
  plot_grid(tag1,
           tag2, 
           ncol = 2, rel_widths = c(.15, .15),align="h"),
  ncol = 1, rel_heights = c(7, .5)
)
plot_with_tags

#10 x 8


### major consolidated recipients map

df_geocoded<-read.csv("consolidated_commitments_geocoded.csv",header=T)

mapWorld <- borders("world", colour="gray83", fill="gray92") 


df <- dplyr::select(df_geocoded, country,Longitude,Latitude,ctotal)

df_sf <- st_as_sf(df, coords=c('Longitude', 'Latitude'), crs = 4326, remove = F)


plot<- ggplot(df_sf)+
  mapWorld +
  geom_point( aes(x = Longitude, y = Latitude, size = ctotal), 
             color = "orange", alpha = 0.6) + 
  geom_text_repel(aes(x = Longitude, y = Latitude, label = country), 
            hjust = .75, vjust = .75, size=11/.pt,point.padding = .02) + 
  scale_size_continuous(range=c(1,9.75), breaks=c(2e9,7e9,1.2e10), name = "Total Aid\n(2011 USD) ",
                        labels = c("2B", "7B", "12B")) +
  theme_void() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.title.x = element_blank(),
         axis.title.y = element_blank())

plot

#save 15 x 9 and crop